require 'pp'

class PositionController < ActionController::Base
	def show
		@position = Position.find(params[:id])
	end

	def move
		#flash[:entered_move] = params[:entered_move]
		# validate move	

		initial_position = Position.find(params[:id])
		begin
			@position = initial_position.make_move(params[:entered_move])
		rescue Exception => e
			#flash[:error] = e.message + e.backtrace.join('<br>')
			flash[:error] = e.message
			redirect_to(:action => "show", :id => initial_position)
			return
		end
		redirect_to :action => "show", :id => @position.id
	end
	
	def drag_move
		entered_move = "#{params[:id]}-#{params[:board_square]}"
		puts "entered_move: #{entered_move}"

		old_position = Position.find(params[:position])
		begin
			@position = old_position.make_move(entered_move)
		rescue Exception => e
			#flash[:error] = e.message + e.backtrace.join('<br>')
			flash[:error] = e.message
			@position = old_position
			# don't know what to do, so just capture exception
		end
		#redirect_to :action => "show", :id => @position.id

		render :partial => "board"
		#@board_square = params[:board_square]
		#@square_color = params[:square_color]
		#render :partial => "board_square"
  end
end
